﻿using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using ExcelDataReader;
using UnityEngine;

public class DialogueReader
{
    public static Dictionary<string, List<TextContent>> dialogueDictionary = new Dictionary<string, List<TextContent>>();
    public static Dictionary<string, TextContent> speakerDictionary = new Dictionary<string, TextContent>();
    //private static List<string> excelList = new List<string>();
    public void GetAllDialogue(string rootPath)
    {
        var dir = new DirectoryInfo(rootPath);
        var fileInfos = dir.GetFileSystemInfos();
        foreach (var file in fileInfos)
        {
            if (file is DirectoryInfo)
            {
                GetAllDialogue(file.FullName);
            }
            else if (file.Extension == ".xlsx")
            {
                //excelList.Add(file.FullName);
                GetDialogue(file.FullName);
            }
        }
    }
    private DataRowCollection ReadExcel(string filePath)
    {
        var file = File.Open(filePath, FileMode.Open, FileAccess.Read, FileShare.Read);
        IExcelDataReader excelDataReader = ExcelReaderFactory.CreateReader(file);
        var result = excelDataReader.AsDataSet().Tables[0].Rows;
        result.Remove(result[0]);
        return result;
    }
    private void GetDialogue(string filePath)
    {
        Debug.Log(filePath);
        var defaultContent = new TextContent();
        string nowLine = "";
        var rows = ReadExcel(filePath);
        foreach (DataRow i in rows)
        {
            var array = i.ItemArray;
            Command command = null;
            if (array[0] is string)
            {
                string cmd = array[0] as string;
                if (cmd[0] == '\\')
                {
                    if ((command = Command.Parse(cmd.Substring(1))) == null)
                    {
                        Debug.LogError("No Command:" + cmd);
                        continue;
                    }
                }
                else if (cmd != "")
                {
                    Debug.Log("加载对话" + cmd + "成功");
                    nowLine = cmd;
                    dialogueDictionary.Add(nowLine, new List<TextContent>());
                    defaultContent.size = (int)(double)array[2];
                    defaultContent.color = (array[3] as string).GetColor();
                    defaultContent.lineBreak = (int)(double)array[4] == 0 ? false : true;
                    defaultContent.typeSpeed = (int)(double)array[5];
                    continue;
                }
            }
            if (array[1] is System.DBNull && command == null) continue;
            int size = array[2] is System.DBNull ? defaultContent.size : (int)(double)array[2];
            Color color = array[3] is System.DBNull ? defaultContent.color : (array[3] as string).GetColor();
            bool lineBreak = array[4] is System.DBNull ? defaultContent.lineBreak : ((int)(double)array[4] == 0 ? false : true);
            int typespeed = array[5] is System.DBNull ? defaultContent.typeSpeed : (int)(double)array[5];
            var tmp = new TextContent(array[1] as string, size, color, lineBreak, typespeed);
            tmp.command = command;
            dialogueDictionary[nowLine].Add(tmp);

        }
    }
}
